﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.IO;
using System.Reflection;
using System.Diagnostics;
/*
Copyright 2011 Mirosław M. Mączka

This file is part of MobiLang.

    MobiLang is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace MobiLang
{
    public partial class Form1 : Form
    {
        private void ClearForm()
        {
            button_Left.Enabled = false;
            button_Right.Enabled = false;
            textBox_FileName.Text = "";
            button_Save.Enabled = false;
            //
            textBox_FileName.Text = toolTip1.GetToolTip(this);
            //
            textBox_InternalName.Text = "";
            textBox_MobiType.Text="";
            //
            textBox_Locale.Text = "";
            listBox_Locale.SelectedIndex = -1;
            //
            textBox_InputLanguage.Text = "";
            listBox_InputLanguage.SelectedIndex = -1;
            //
            textBox_OutputLanguage.Text = "";
            listBox_OutputLanguage.SelectedIndex = -1;
            //
            textBox_FullName.Text = "";
            textBox_EXTHflags.Text = "";
            //
            listBox_EXTH.Items.Clear();
            //
        }
        private void DisplayFile(ebook ek)
        {
            try
            {
                //mh.FileName = "Cierpienia mlodego Wertera - Goethe_ Johann Wolfgang von.mobi";
                mh.FileName = ek.PathName;
                //
                mh.ReadHeader();
                if (!mh.FileOK)
                {
                    Enable(false);
                    return;
                }
                Enable(true);
                //
                textBox_FileName.Text = string.Format("{0}/{1}  [{2}]", idxFiles+1, files.Count, ek.ShortName);
                //
                textBox_InternalName.Text = mh.InternalName;
                textBox_MobiType.Text = ClMobiType.ToText(mh.MobiType);
                //
                if (mh.MobiHeaderOK)
                {
                    textBox_Locale.Text = mh.Locale.ToString();
                    setLBbyValue(listBox_Locale, mh.Locale);
                    //
                    textBox_InputLanguage.Text = mh.InputLanguage.ToString();
                    setLBbyValue(listBox_InputLanguage, mh.InputLanguage);
                    //
                    textBox_OutputLanguage.Text = mh.OutputLanguage.ToString();
                    setLBbyValue(listBox_OutputLanguage, mh.OutputLanguage);
                    //
                    textBox_FullName.Text = mh.FullName;
                    textBox_EXTHflags.Text = mh.ExthFlags.ToString();
                }
                button_Save.Enabled = false;
                //
                if (mh.ExthHeaderOK)
                {
                    listBox_EXTH.BeginUpdate();
                    listBox_EXTH.Items.Clear();
                    foreach (string s in mh.Exths)
                    {
                        listBox_EXTH.Items.Add(s);
                    }
                    listBox_EXTH.EndUpdate();
                }
                //
            }
            catch (Exception e)
            {
                ClearForm();
                MessageBox.Show(e.Message, "ERROR");
            }
        }

        // Uwaga: w XP nie ma w systemie pojedynczych kodów językowych (pl), tylko pełne (pl-pl)
        private static void setLBbyValue(ListBox c, int v)
        {
            c.Enabled = false;
            c.BeginUpdate();
            //
            bool found = false;
            for (int i = 0; i < c.Items.Count; i++)
            {
                c.SelectedIndex = i;
                if (((LanguageCodes.Lang)c.SelectedItem).Lcid == v){
                    found = true;
                    c.TopIndex = i;
                    break;
                }
            }
            if (!found)// XP?
            {
                for (int i = 0; i < c.Items.Count; i++)
                {
                    c.SelectedIndex = i;
                    if ((((LanguageCodes.Lang)c.SelectedItem).Lcid&255) == v)
                    {
                        found = true;
                        c.TopIndex = i;
                        break;
                    }
                }
                //
                int vv = LanguageCodes.RecodeLcid(v);// próbujemy przekodować LCID, dla 1034 es-es, 
                if (0 != vv)
                {
                    for (int i = 0; i < c.Items.Count; i++)
                    {
                        c.SelectedIndex = i;
                        if (((LanguageCodes.Lang)c.SelectedItem).Lcid == vv)
                        {
                            found = true;
                            c.TopIndex = i;
                            break;
                        }
                    }
                }

                if (!found)// XP, brak tego języka, ustawiamy Unknown
                {
                    for (int i = 0; i < c.Items.Count; i++)
                    {
                        c.SelectedIndex = i;
                        if (((LanguageCodes.Lang)c.SelectedItem).Lcid == 0)
                        {
                            found = true;
                            c.TopIndex = i;
                            break;
                        }
                    }
                }
            }
            c.EndUpdate();
            c.Enabled = true;
        }

        /*
        private static void setLBbyString(ListBox lb, string s)
        {
            lb.Enabled = false;
            lb.BeginUpdate();
            //
            for (int i = 0; i < lb.Items.Count; i++)
            {
                if(((LanguageCodes.Lang)lb.SelectedItem).Name.StartsWith(s))
                {
                    lb.SelectedIndex = i;
                    lb.TopIndex = i;
                    break;
                }
            }
            lb.EndUpdate();
            lb.Enabled = true;
        }
        */

        public void ShowNewFiles()
        {
            idxFiles = 0;
            ClearForm();
            //
            if (files.Count < 2)
            {
                button_Left.Enabled = false; button_Right.Enabled = false;
            }
            else
            {
                button_Left.Enabled = false; button_Right.Enabled = true;
            }
            //
            if (files.Count > 0)
            {
                DisplayFile(files[idxFiles]);
                //
                if (!mh.FileOK) return;
            }
            else
            {
                idxFiles = 0;
                //
                Enable(false);
            }
        }
        public void ShowFileRight()
        {
            if (files.Count <= 1) return;
            //
            if (idxFiles >= (files.Count-1))//ostatni
            {
                button_Left.Enabled = true;
                button_Right.Enabled = false;
                return;
            }
            //
            if (idxFiles > 0) button_Left.Enabled = true;
            //
            idxFiles++;
            //
            DisplayFile(files[idxFiles]);
            //
            if (idxFiles >= (files.Count - 1))//ostatni
            {
                button_Left.Enabled = true;
                button_Right.Enabled = false;
            }
        }
        public void ShowFileLeft()
        {
            if (files.Count <= 1) return;
            //
            if (idxFiles <= 0)//pierwszy
            {
                button_Left.Enabled = false;
                button_Right.Enabled = true;
                return;
            }
            //
            if (idxFiles < (files.Count - 1)) button_Right.Enabled = true;
            //
            idxFiles--;
            //
            DisplayFile(files[idxFiles]);
            //
            if (idxFiles <= 0)//pierwszy
            {
                button_Left.Enabled = false;
                button_Right.Enabled = true;
            }
        }
        public static bool IsFirstTime()
        {
            bool retVal = false;
            //
            if (true == Properties.Settings.Default.IsFirstTime)
            {
                Properties.Settings.Default.IsFirstTime = false;
                //
                //Help.ShowHelpIndex(this, helpProvider1.HelpNamespace);
                //MessageBox.Show(labelInfoText.Text, labelInfoCaption.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                //
                return true;
            }
            //
            return retVal;
        }
        private void Enable(bool v)
        {
            splitContainer1.Enabled = v;
        }


        private string getVersionInfo()
        {
            //
            string cc = System.Globalization.CultureInfo.CurrentCulture.TextInfo.CultureName;
            //
            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            {
                //działa tylko na zainstalowanym sieciowo programie!
                return string.Format("Version: {0} ({1})", System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion, cc);
            }
            //
            Assembly ass = Assembly.GetExecutingAssembly();
            if (ass != null)
            {
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(ass.Location);
                return String.Format("Version: {0} ({1})", fvi.ProductVersion, cc);
            }
            //
            return "Version: unknown";
        }

    }//class
}//namespace
